package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.client.LogininfoClient;
import cn.itsource.ymcc.domain.Employee;
import cn.itsource.ymcc.domain.Logininfo;
import cn.itsource.ymcc.mapper.EmployeeMapper;
import cn.itsource.ymcc.result.JsonResult;
import cn.itsource.ymcc.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whale.chen
 * @since 2023-02-23
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    private LogininfoClient logininfoClient;
    /**
     * 保存时 员工也要生成对应的logininfo表
     * @param entity
     * @return
     */
    @Override
    public boolean save(Employee entity) {
        //数据库中是否已经有登录的这个角色
//        QueryChainWrapper<Employee> wrapper = new QueryChainWrapper<>(baseMapper);
//        Employee real_name = baseMapper.selectOne(wrapper.eq("real_name", entity.getRealName()));

        Logininfo logininfo = new Logininfo()
                .setUsername(entity.getRealName())

                .setPassword(new BCryptPasswordEncoder().encode(entity.getTel()))//使用手机号作为默认密码
                .setType(0);//后台默认是管理员
        JsonResult jsonResult = logininfoClient.saveOrUpdate(logininfo);
        Long loginId = ((Integer)jsonResult.getData()).longValue();
        entity.setLoginId(loginId);
        super.save(entity);
        return true;
    }
}
